"""地址管理数据模型"""
from datetime import datetime
from decimal import Decimal
from typing import Optional

from pydantic import BaseModel, Field, ConfigDict


class BaseAddress(BaseModel):
    """统一地址管理模型"""
    id: int = Field(..., description="主键ID")
    parent_id: Optional[int] = Field(None, description="父级地址ID")
    name: str = Field(..., max_length=128, description="地址名称")
    full_name: str = Field(..., max_length=512, description="完整地址")
    level: int = Field(..., description="地址级别: 1-国家, 2-省, 3-市, 4-区县, 5-街道, 6-社区, 7-详细地址")
    region_code: Optional[str] = Field(None, max_length=12, description="行政区划码(兼容国标9/12位)")
    longitude: Decimal = Field(..., description="经度")
    latitude: Decimal = Field(..., description="纬度")
    source: str = Field("CUSTOM", max_length=16, description="数据来源: CUSTOM/GB/T-SC/SG-CIM")
    is_active: bool = Field(True, description="是否启用")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")

    model_config = ConfigDict(from_attributes=True)


class DeviceAddress(BaseModel):
    """设备地址关联模型"""
    id: int = Field(..., description="主键ID")
    device_type: str = Field(..., max_length=24, description="设备类型(STATION,BOX,RTU,METER)")
    device_id: int = Field(..., description="设备ID")
    address_id: int = Field(..., description="地址ID")
    region_code: Optional[str] = Field(None, max_length=6, description="冗余区县码(6位)")
    address_type: str = Field("INSTALLATION", max_length=24, description="地址类型:安装地址/维护地址/运营地址")
    deleted: Optional[bool] = Field(False, description="是否删除")
    is_primary: bool = Field(False, description="是否主地址")
    description: Optional[str] = Field(None, max_length=255, description="地址描述")
    created_at: datetime = Field(..., description="创建时间")
    ended_at: Optional[datetime] = Field(None, description="结束时间")

    model_config = ConfigDict(from_attributes=True)


class DeviceWithAddress(BaseModel):
    """设备带地址信息的模型"""
    address_id: Optional[int] = Field(None, description="地址ID")
    address_name: Optional[str] = Field(None, description="地址名称")
    full_address: Optional[str] = Field(None, description="完整地址")
    longitude: Optional[Decimal] = Field(None, description="经度")
    latitude: Optional[Decimal] = Field(None, description="纬度")
    address_type: Optional[str] = Field(None, description="地址类型")
    is_primary: Optional[bool] = Field(None, description="是否主地址")

    model_config = ConfigDict(from_attributes=True)